package com.huawei.esdk.uc.sametime.chatwindow.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;

import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.ibm.collaboration.realtime.chat.actions.ChatActionWindowDelegate;
import com.ibm.collaboration.realtime.chatwindow.ChatWindowHandler;

/**
 * 在聊天窗口上发起会议时候所用的控制类action
 * @author y00175154
 */

public class EsdkConfChatAddOnOpenAction implements ChatActionWindowDelegate
{

    public EsdkConfChatAddOnOpenAction()
    {
    }

    public void run(IAction action, ChatWindowHandler handler)
    {
        /*
         * 总体思路是如果当前聊天窗口中已经打开了弹出区域,则认为是已经有一个通话存在了
         * 如果没有打开弹出区域,则需要判断聊天窗口之外是否有通话,如果没有的话,则可以在聊天窗口右下侧显示弹出区域并发起呼叫
         */
        if (!handler.isPopupVisible())
        {
            // 这里先判断UC是否登录,不用判断sametime是否登录了,因为sametime未登录,很多功能都被限制不能使用了.
            boolean isucLogin = GlobalConfiguration.getInstance().isUCLogin();
            boolean isInCall = ConstantsDefines.CallStatus.end != UCCallService
                    .getCallStatus();
            if (isucLogin)
            {
                if (!isInCall)
                {
                    GlobalConfiguration.getInstance().setVideo(false);
                    GlobalConfiguration.getInstance().setConf(true);
                    handler
                            .createPopupMessageArea(
                                    "com.huawei.esdk.uc.sametime.chatwindow.chatwindowConvPopupAddOn",
                                    300);
                }
                else
                {
                    MessageDialog.openInformation(handler.getShell(),
                            Messages.ERROR_TIP, Messages.IS_CALLING);
                }

            }
            else
            {
                MessageDialog.openInformation(handler.getShell(),
                        Messages.ERROR_TIP,
                        Messages.EsdkConfChatAddOnOpenAction_UC_NOT_LOGIN);
            }

        }
        else
        {
            MessageDialog
                    .openError(
                            handler.getShell(),
                            Messages.ERROR_TIP,
                            Messages.EsdkConfChatAddOnOpenAction_CANNOT_OPEN_SECOND_WINDOW);
        }
    }

}
